package com.lk.jmeter.socket;

//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.io.PrintWriter;
//import java.net.Socket;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class SocketTest extends AbstractJavaSamplerClient {
    // Sock begin----------------------------------------------
//    private Socket sid;
//    private PrintWriter out;
//    private BufferedReader in;
    private String ip;
    private String port;
    private String serial;
    private String business;
    private String outlets;
    private String code;
    private String data;
    private String type;
    private String methodType;
    // Sock end------------------------------------------------

    private static String label = "howsky.net";

    // 测试结果
    private SampleResult sr;
    private String resultData;

    /**
     * 初始化
     */
    public void setupTest(JavaSamplerContext arg0) {
//        System.out.println("setupTest");
    }

    /**
     * 设置请求的参数
     */
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("ip", "127.0.0.1");
        params.addArgument("port", "3131");
        params.addArgument("type", "3");
        params.addArgument("methodType", "1");
        params.addArgument("serial", "E1K31EA000097");
        params.addArgument("business", "nmg101");
        params.addArgument("outlets", "08079");
        params.addArgument("code", "1001");
        params.addArgument("data", "[{'account':'62220214053210','ID_num':'330326199105140622',"
                + "'mobile':'18259067777','name':'张三'}]");
//        params.addArgument("msg", "{'serial':'dev_1','business':'1001','outlets':'000002',"
//                + "'data':[{'account':'62220214053210','ID_num':'330326199105140622',"
//                + "'mobile':'18259067777','name':'张三'}]}");
        return params;
    }

    /**
     * 运行过程
     */
    public SampleResult runTest(JavaSamplerContext arg0) {
        ip = arg0.getParameter("ip");
        port = arg0.getParameter("port");
        
        serial = arg0.getParameter("serial");
        business = arg0.getParameter("business");
        outlets = arg0.getParameter("outlets");
        code = arg0.getParameter("code");
        data = arg0.getParameter("data");
        
        type = arg0.getParameter("type");
        methodType = arg0.getParameter("methodType");
        String msg = null;
        switch (Integer.parseInt(type)) {
		case 1:{
			msg = "{'serial':'"+serial+"'}";
			break;
		}
		case 3:{
			msg = "{'serial':'"+serial+"','business':'"+business+"','outlets':'"+outlets+"',"
			          + "'code':'"+code+"','data':"+data+"}";
			break;
		}
		default:
			break;
		}
        
        // 结果树内容
        sr = new SampleResult();
        sr.setSampleLabel(label);
        try {
            sr.sampleStart(); // 记录程序执行时间，以及执行结果
            // 发送数据
            resultData = sendMsg(ip, Integer.parseInt(port), Integer.parseInt(type), Integer.parseInt(methodType), msg);
            sr.setSamplerData(msg);
//            if(resultData.indexOf("surplus") < 0) {
//                sr.setSuccessful(false);
//            } else {
                sr.setSuccessful(true);
//            }
           
            sr.setResponseData("结果是：" + resultData ,null);
            sr.setDataType(SampleResult.TEXT);
        } catch (Throwable e) {
            sr.setSuccessful(false);
        } finally {
            sr.sampleEnd();
        }
        return sr;
    }

    /**
     * 结束
     */
    public void teardownTest(JavaSamplerContext arg0) {
    }

    /**
     * 发送消息
     * 
     * @param ip
     * @param port
     * @param msg
     * @throws Exception
     */
    private String sendMsg(String ip, int port, int type, int methodType, String msg) throws Exception {
        
        
        
//        sid = new Socket(ip, port);
//        in = new BufferedReader(new InputStreamReader(sid.getInputStream()));
//        out = new PrintWriter(sid.getOutputStream(), true);
//        out.println(msg);
//        StringBuffer sb = new StringBuffer();
//        String line = in.readLine();
//        while(line != null) {
//            sb.append(line);
//            line = in.readLine();
//        }
        return BusinessRecordTest.sendMsg(ip, port, type, methodType, msg);
    }
}
